#include <iostream>
#include <unistd.h>
#include "com.hpp"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>

int main()
{
    umask(0);
    int ret=mkfifo(FIFO_FILE,0666);
    if(ret!=0)
    {
        std::cerr<<"创建命名管道失败"<<std::endl;
        return 1;
    }
    int fd=open(FIFO_FILE,O_RDONLY);
    if(fd<0)
    {
        std::cerr<<"打开文件失败"<<std::endl;        
    }
    // std::cout<<"阻塞在open"<<std::endl;
    char buffer[1024];
    while(true)
    {
        
        ssize_t res=read(fd,buffer,sizeof(buffer)-1);
        if(res>0)
        {
            buffer[res]=0;
            std::cout<<"client 发送了"<<buffer<<std::endl;
        }
        //如果先关闭了client端，就会处于一直读取的状态，此时就会阻塞在open内部
        else
        {
            std::cout<<"quit"<<std::endl;
        }
    }
    close(fd);
    return 0;
}

